package com.cyc.basic.controller.rule;

import com.cyc.basic.pojo.vo.OrderInfoTest;
import com.cyc.basic.pojo.vo.PayResult;
import com.cyc.basic.service.rule.IPayment;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 支付服务
 * @author fei
 */
@RestController
public class PayController {

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 支付接口
     * @param amount
     * @param paymentType
     * @return
     */
    @ApiOperation("测试策略模式支付方式选择")
    @RequestMapping("/pay")
    public PayResult pay(@RequestParam("amount") int amount,
                         @RequestParam("paymentType") String paymentType) {
        OrderInfoTest orderInfo = new OrderInfoTest();
        orderInfo.setAmount(amount);
        orderInfo.setPaymentType(paymentType);

        // 根据支付类型获取对应的策略 bean
        IPayment payment = applicationContext.getBean(orderInfo.getPaymentType(), IPayment.class);

        // 开始支付
        PayResult payResult = payment.pay(orderInfo);
        return payResult;
    }

}